package tt.dz.biz.service.user;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tt.dz.biz.dao.user.IdentityAccountDAO;
import tt.dz.biz.service.cardno.CardNoProduct;
import tt.dz.biz.service.common.CommonService;
import tt.dz.common.BusinessException;
import tt.dz.common.DZStringUtil;
import tt.dz.common.DateUtil;
import tt.dz.common.DzConstants;
import tt.dz.common.DzConstants.bizSysCode;
import tt.dz.common.DzConstants.cardState;
import tt.dz.common.DzConstants.userLock;
import tt.dz.common.DzConstants.userRegistChannelType;
import tt.dz.common.DzConstants.userType;
import tt.dz.common.DzConstants.userValid;
import tt.dz.common.DzDigester;
import tt.dz.common.DzErrorCode;
import tt.dz.common.DzErrorCode.ExceptionErrorCode;
import tt.dz.common.LogUtil;
import tt.dz.vo.IdentityAccountVO;
import tt.dz.vo.UserBaseVO;
import tt.dz.vo.UserDetailVO;

@Service("identityAccountService")
public class IdentityAccountService
{
  @Resource(name="identityAccountDAO")
  IdentityAccountDAO identityAccountDAO;
  @Resource(name="userService")
  UserService userService;
  @Resource(name="commonService")
  private CommonService commonService;
  @Resource(name="offlineIdentityCardHisService")
  private OfflineIdentityCardHisService offlineIdentityCardHisService;
  @Value("${identityAccountKey}")
  private String identityAccountKey;
  
  public String getIdentityAccountKey()
  {
    return this.identityAccountKey;
  }
  
  public String getIdentityAccountReportDir()
  {
    return this.commonService.getDownLoadDir();
  }
  
  public UserBaseVO queryUserByIdentityAccount(IdentityAccountVO identityAccountVO)
  {
    UserBaseVO userBaseVO = null;
    try
    {
      userBaseVO = this.identityAccountDAO.queryUserByIdentityAccount(identityAccountVO);
    }
    catch (Exception e)
    {
      LogUtil.error("根据身份识别卡查询关联的用户异常--入参" + identityAccountVO.getCardNo(), e);
      
      throw new BusinessException(DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.value(), DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.getDesc(), e.getMessage());
    }
    return userBaseVO;
  }
  
  public boolean insertIdentityAccount(IdentityAccountVO identityAccountVO)
  {
    boolean excuteFlag = false;
    try
    {
      int i = this.identityAccountDAO.insertIdentityAccount(identityAccountVO);
      excuteFlag = i > 0;
      if (excuteFlag) {
        this.offlineIdentityCardHisService.insertIdentityAccount(identityAccountVO);
      }
    }
    catch (Exception e)
    {
      LogUtil.error(" 插入身份识别卡对象异常--入参：" + identityAccountVO.toString(), e);
    }
    return excuteFlag;
  }
  
  public List<IdentityAccountVO> listAllIdentityAccount()
  {
    List<IdentityAccountVO> identityAccountList = null;
    try
    {
      identityAccountList = this.identityAccountDAO.listAllIdentityAccount();
    }
    catch (Exception e)
    {
      LogUtil.error("查询所有身份识别卡对象异常", e);
      identityAccountList = new ArrayList();
    }
    return identityAccountList;
  }
  
  public List<IdentityAccountVO> queryIdentityAccount(IdentityAccountVO identityAccountVO)
  {
    List<IdentityAccountVO> identityAccountList = null;
    try
    {
      identityAccountList = this.identityAccountDAO.queryIdentityAccount(identityAccountVO);
    }
    catch (Exception e)
    {
      LogUtil.error("根据传入条件查询身份验证卡异常--入参：" + identityAccountVO.toString(), e);
      identityAccountList = new ArrayList();
    }
    return identityAccountList;
  }
  
  public List<IdentityAccountVO> queryIdentityAccountAndConvert(IdentityAccountVO identityAccountVO)
  {
    List<IdentityAccountVO> identityAccountList = null;
    try
    {
      identityAccountList = this.identityAccountDAO.queryIdentityAccountAndConvert(identityAccountVO);
      for (IdentityAccountVO i : identityAccountList) {
        i.setPassword(DzDigester.desDecrypt(i.getPassword(), this.identityAccountKey));
      }
    }
    catch (Exception e)
    {
      LogUtil.error("根据传入条件查询身份验证卡异常--入参：" + identityAccountVO.toString(), e);
      identityAccountList = new ArrayList();
    }
    return identityAccountList;
  }
  
  public boolean createIdentityAccount(CardNoProduct cardNoProduct, String companyCode)
  {
    boolean excuteFlag = false;
    try
    {
      String managerId = this.identityAccountDAO.getSystemCurrentOper();
      UserBaseVO baseVo = new UserBaseVO();
      String userId = this.userService.getUserId(DzConstants.userType.UNIFIEDPAY_CUSTOMER, DzConstants.userRegistChannelType.MAINSERVER);
      
      baseVo.setUserId(userId);
      baseVo.setMerchantCode(cardNoProduct.getMerchantCode());
      baseVo.setIsLocked(DzConstants.userLock.UNLOCK.value());
      baseVo.setIsValid(DzConstants.userValid.VALID.value());
      baseVo.setSalt(DZStringUtil.getRandomString(6));
      baseVo.setPassword(DzDigester.md5ForUser(userId, baseVo.getSalt()));
      baseVo.setUserType(DzConstants.userType.UNIFIEDPAY_CUSTOMER.value());
      baseVo.setRegistTime(DateUtil.getDBCurrentTime());
      baseVo.setEnterpriseCode(companyCode);
      UserDetailVO detailVO = new UserDetailVO();
      detailVO.setUserId(userId);
      

      IdentityAccountVO identityAccountVO = new IdentityAccountVO();
      identityAccountVO.setUserId(userId);
      identityAccountVO.setCardBin(cardNoProduct.getBin());
      identityAccountVO.setCardType(cardNoProduct.getCardType());
      identityAccountVO.setAreaCode(cardNoProduct.getArea());
      identityAccountVO.setMerchantCode(cardNoProduct.getMerchantCode());
      identityAccountVO.setCheckCode(cardNoProduct.getCheckCode());
      identityAccountVO.setCardNo(cardNoProduct.getCardNo());
      identityAccountVO.setState(DzConstants.cardState.ACTIVATE.value());
      identityAccountVO.setInitDate(DateUtil.getDBCurrentTime());
      identityAccountVO.setInitOper(managerId);
      identityAccountVO.setOpenChannel(DzConstants.bizSysCode.MAIN_SERVER.value());
      identityAccountVO.setOpenDate(DateUtil.getDBCurrentTime());
      identityAccountVO.setOpenOper(managerId);
      identityAccountVO
        .setPassword(DzDigester.desEncrypt(DZStringUtil.getRandomString(6), this.identityAccountKey));
      this.userService.saveregister(baseVo, detailVO);
      insertIdentityAccount(identityAccountVO);
      excuteFlag = true;
    }
    catch (Exception e)
    {
      LogUtil.error("建立身份验证卡及关联用户出现异常", e);
    }
    return excuteFlag;
  }
  
  public boolean bacthUpdateIdentityAccount(List<IdentityAccountVO> identityAccountList)
  {
    boolean excuteFlag = true;
    try
    {
      int[] iArray = this.identityAccountDAO.bacthUpdateIdentityAccount(identityAccountList);
      for (int i : iArray) {
        if (i < 0)
        {
          excuteFlag = false;
          throw new BusinessException();
        }
      }
      this.offlineIdentityCardHisService.batchInsertOfflineIdentityCardHis(identityAccountList);
    }
    catch (Exception e)
    {
      LogUtil.error(" 批量更新身份识别卡对象异常--入参：" + identityAccountList.toString(), e);
    }
    return excuteFlag;
  }
  
  public IdentityAccountVO queryIdentifyCardByNo(String cardNo)
    throws Exception
  {
    try
    {
      return this.identityAccountDAO.queryIdentifyCardByNo(cardNo);
    }
    catch (Exception e)
    {
      LogUtil.error("关联查询身份识别卡异常，参数为cardNo：" + cardNo, e);
      throw e;
    }
  }
}
